package yunwo.cn.yzstation.view.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Request;
import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.api.HttpManager;
import yunwo.cn.yzstation.api.okapi.GLCallBack;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.api.okapi.LogICall;

import yunwo.cn.yzstation.base.MyBaseActivity;
import yunwo.cn.yzstation.bean.LoginBean;

/**
 * Created by lao on 2017/9/6.
 */

public class LoginActivity extends MyBaseActivity implements View.OnClickListener{

    private TextView mRegistered;
    private Intent mIntent;
    private EditText mPawd;
    private EditText mAccount;
    private TextView mSubmit;
    private String mUserphone;
    private String mPawd1;
    private ImageView mPasswordControl;
    private boolean isPasswordControlClicked = false;
    private ProgressDialog mMyDialog;
    private String mMobile = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            initView();
        if (getIntent().getExtras() != null) {
            mMobile = getIntent().getExtras().getString("mobile", "");
            mAccount.setText(mMobile);
        }
    }


    private void initView() {
        mRegistered = (TextView) findViewById(R.id.registered);
        mPawd = (EditText) findViewById(R.id.pawd);
        mAccount = (EditText) findViewById(R.id.account);
        mSubmit = (TextView) findViewById(R.id.submit);
        mPasswordControl = (ImageView) findViewById(R.id.passwordControl);
        mPasswordControl.setOnClickListener(this);
        mRegistered.setOnClickListener(this);
        mSubmit.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (getIntent().getStringExtra("userphone")!=null&&getIntent().getStringExtra("pawd")!=null) {
            mUserphone = getIntent().getStringExtra("userphone");
            mPawd1 = getIntent().getStringExtra("pawd");
            mPawd.setText(mPawd1);
            mAccount.setText(mUserphone);
        }
        if (getIntent().getStringExtra("logout") != null) {
            String logout = getIntent().getStringExtra("logout");
            if (logout.equals("1")) {
                finishother();
            }
        }

    }

    @Override
    protected String settiltile() {
        return null;
    }

    @Override
    protected int getLayoutRsid() {
        return R.layout.activity_login;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.passwordControl:
                isPasswordControlClicked = !isPasswordControlClicked;
                if (isPasswordControlClicked) {
                    //true 明文
                    mPasswordControl.setImageResource(R.drawable.btn_password_pre);
                    mPawd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    mPawd.setSelection(mPawd.length());
                } else {
                    //false 密文
                    mPawd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    mPasswordControl.setImageResource(R.drawable.btn_password_nor);
                    mPawd.setSelection(mPawd.length());
                }
                break;
            case R.id.registered:
                mIntent = new Intent(LoginActivity.this,RegistActivity.class);
                startActivity(mIntent);
                break;
            case R.id.submit:
                if (mAccount.getText().toString().trim().equals("")) {
                    Toast.makeText(LoginActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mPawd.getText().toString().trim().equals("")) {
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                final Map<String,Object> params = new HashMap<>();

                final String trim1 = mAccount.getText().toString().trim();
                params.put("loginType", "1");
                params.put("password", mPawd.getText().toString().trim());
                params.put("mobile", mAccount.getText().toString().trim());

                HttpManager.hpptPostString3(MyConstants.login,params).execute(new GLCallBack() {
                    @Override
                    public void onResponseT(String result, int code, String msg, int id) {
                        if (code == 200) {
                            if (result.length() > 0) {
                                LoginBean.DataBean loginBean = new Gson().fromJson(result, LoginBean.DataBean.class);
                                SharedPreferences information = getSharedPreferences("information", Context.MODE_PRIVATE);
                                SharedPreferences.Editor informationedit = information.edit();
                                informationedit.putString("mobile", trim1);
                                informationedit.putString("token", loginBean.getAccess_token());
                                informationedit.putString("tokentype", loginBean.getToken_type());
                                informationedit.putString("Authorization",
                                        loginBean.getToken_type() + " " + loginBean.getAccess_token());
                                informationedit.commit();
                                Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                            }
                        }
                        else {
                            Toast.makeText(LoginActivity.this,  msg, Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(LoginActivity.this,"服务器连接失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAfter(int id) {
                        if (mMyDialog != null && mMyDialog.isShowing()) {
                            mMyDialog.dismiss();
                        }
                    }

                    @Override
                    public void onBefore(Request request, int id) {
                        if (mMyDialog == null) {
                            mMyDialog = ProgressDialog.show(LoginActivity.this, null, "请稍等...");
                            mMyDialog.setCancelable(false);
                            mMyDialog.show();
                        }
                    }
                });
             /*   OkHttpUtils.post().url(MyConstants.login).params(params)
                        .build().execute(new GLCallBack() {
                    @Override
                    public void onResponseT(String result, int code, String msg, int id) {
                        if (code == 200) {
                            if (result.length() > 0) {
                                LoginBean.DataBean loginBean = new Gson().fromJson(result, LoginBean.DataBean.class);
                                SharedPreferences information = getSharedPreferences("information", Context.MODE_PRIVATE);
                                SharedPreferences.Editor informationedit = information.edit();
                                informationedit.putString("mobile", trim1);
                                informationedit.putString("token", loginBean.getAccess_token());
                                informationedit.putString("tokentype", loginBean.getToken_type());
                                informationedit.putString("Authorization",
                                        loginBean.getToken_type() + " " + loginBean.getAccess_token());
                                informationedit.commit();
                                Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                            }
                        }
                        else {
                            Toast.makeText(LoginActivity.this, code + msg, Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(LoginActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                    }
                });*/
                break;
        }
    }


}
